#include <QtGui>
#include "chartParts.h"

ChartParts::ChartParts(TrayToolKit * new_ttk, QWidget *parent) : QWidget(parent) {

  resetItems();
  insCurItem = 0;
  xParts[0] = 0;

  ttk = new_ttk;

} //ChartParts::ChartParts(TrayToolKit * new_ttk, QWidget *parent) : QWidget(parent)

void ChartParts::setCurItem(int num) { 

  insCurItem = num; 
  update(); 

} //void ChartParts::setCurItem(int num)

void ChartParts::paintEvent(QPaintEvent *) {

  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing, true);
  draw(&painter);

} //void ChartParts::paintEvent(QPaintEvent * event)

void ChartParts::drawCells(QPainter * painter) {

  QPen pen = painter->pen();
  painter->setPen(QPen(QColor(180, 180, 180)));

  int beg;
  int end;

  //Y
  beg = DEFSIZECELL2+DEFXSHIFTPARTS;
  end = DEFWRECTPARTS+DEFXSHIFTPARTS;
  for (int i=beg; i < end; i+=DEFSIZECELL2)
    painter->drawLine(i, DEFYSHIFTPARTS+1, i, DEFHRECTPARTS+DEFYSHIFTPARTS-1);

  //X
  beg = DEFHRECTPARTS+DEFYSHIFTPARTS;
//   beg = DEFHRECTPARTS-DEFSIZECELL2+DEFYSHIFTPARTS;
  end = 0+DEFYSHIFTPARTS;
  for (int j=beg; j > end; j-=DEFSIZECELL2)
    painter->drawLine(1+DEFXSHIFTPARTS, j, DEFWRECTPARTS+DEFXSHIFTPARTS-1, j);

  painter->setPen(QPen(QColor(Qt::black)));
  painter->drawLine(0+DEFXSHIFTPARTS, 0+DEFYSHIFTPARTS, DEFWRECTPARTS+DEFXSHIFTPARTS, 0+DEFYSHIFTPARTS);
  painter->setPen(pen);

} //void ChartParts::drawCells(QPainter * painter)

void ChartParts::drawItems(QPainter * painter) {

  QPen oldPen = painter->pen();
  QPen pen;
  QBrush brush;
  painter->setPen(QColor(Qt::black));
  painter->setBrush(QBrush(QColor(150, 150, 150)));

  QPolygon pol(4);

  int    heightItem   = 4;
  // int    heightItem_r = 3;
  double allLength    = length();
  double lenChart     = DEFWRECTPARTS;
  double allInc       = inclination();
  double allInc_r     = inclination_r();
  double incChart     = DEFHRECTPARTS-20;
  int    count	      = itemCount();
  int 	 curPosX      = 0+DEFXSHIFTPARTS;
  int	 newPosX      = 0;
  int 	 curPosY      = 10+DEFYSHIFTPARTS;
  int 	 newPosY      = 0;
  int 	 curPosY_r    = 10+DEFYSHIFTPARTS;
  int 	 newPosY_r    = 0;

  int 	 numItem      = 0;

  int 	 deltaLight   = 0;

  int 	 sepPoint1    = 0;
  int 	 sepPoint2    = 0;

  int 	 height_Hr    = 0;
  int 	 height_Hcr   = 0;
  int 	 height_Hrain = 0;
  
  int 	 transparency = 95;

  for (int num = 0; num < MAXPARTSITEMS; num++) {

    if (items[num] != NULL) {

      ttk->setVar(items[num]->F(), VAR_F);
      ttk->setVar(items[num]->i(), VAR_I);
      ttk->setVar(items[num]->i(), VAR_IR);

      count--;

      if (count == 0) {

      	newPosX   = DEFWRECTPARTS+DEFXSHIFTPARTS;
      	newPosY   = DEFHRECTPARTS-heightItem-10+DEFYSHIFTPARTS;
      	newPosY_r = DEFHRECTPARTS-heightItem-10+DEFYSHIFTPARTS;

      } //if (count == 0)

      else {

      	if (allLength != 0)
      	  newPosX   = curPosX   + rint(lenChart * (items[num]->length() / allLength  ));
      	else
      	  newPosX   = curPosX;
      	
      	if (allInc != 0)
      	  newPosY   = curPosY   + rint(incChart * (items[num]->i()      / allInc     ));
      	else
      	  newPosY   = curPosY;

      	if (allInc_r != 0)
      	  newPosY_r = curPosY_r + rint(incChart * (items[num]->ir()     / allInc_r   ));
      	else
      	  newPosY_r = curPosY_r;

      } //else: if (count == 0)

      numItem++;
      xParts[numItem] = newPosX; 

      sepPoint1 = (rint((newPosX - curPosX)/3))*2;
      sepPoint2 = (rint((newPosX - curPosX)/3))*1;

      height_Hr    = rint( (DEFHRECTPARTS) * ttk->getVar(VAR_HR) / ttk->getVar(VAR_H) );
      height_Hcr   = rint( (DEFHRECTPARTS) * ttk->crHeightTrapTray()  / ttk->getVar(VAR_H) );
      height_Hrain = rint( (DEFHRECTPARTS) * ttk->getVar(VAR_HRAIN)   / ttk->getVar(VAR_H) );

      //Hr
      pol.setPoint(0, curPosX            , DEFHRECTPARTS + DEFYSHIFTPARTS      );
      pol.setPoint(1, curPosX            , DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hr );
      pol.setPoint(2, newPosX - sepPoint1, DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hr );
      pol.setPoint(3, newPosX - sepPoint1, DEFHRECTPARTS + DEFYSHIFTPARTS      );

      brush = painter->brush();
      if (num == curItem())
      	painter->setBrush(QBrush(QColor(30, 30, 150, transparency)));
      else
      	painter->setBrush(QBrush(QColor(20, 20, 130, transparency)));

      // pen.setColor(QColor(120, 120, 120));
      pen.setStyle(Qt::NoPen);
      // pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);

      if (items[num]->length() > 0)
	      painter->drawPolygon(pol);

      // pen.setColor(QColor(40, 40, 40));
      pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);
      
      //Hcr
      pol.setPoint(0, curPosX + sepPoint2, DEFHRECTPARTS + DEFYSHIFTPARTS      );
      pol.setPoint(1, curPosX + sepPoint2, DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hcr );
      pol.setPoint(2, newPosX - sepPoint2, DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hcr );
      pol.setPoint(3, newPosX - sepPoint2, DEFHRECTPARTS + DEFYSHIFTPARTS      );

      brush = painter->brush();
      if (num == curItem())
      	painter->setBrush(QBrush(QColor(150, 30, 30, transparency)));
      else
      	painter->setBrush(QBrush(QColor(130, 20, 20, transparency)));

      // pen.setColor(QColor(120, 120, 120));
      pen.setStyle(Qt::NoPen);
      // pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);

      if (items[num]->length() > 0)
	      painter->drawPolygon(pol);

      // pen.setColor(QColor(40, 40, 40));
      pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);

      //Hrain
      pol.setPoint(0, curPosX + sepPoint1, DEFHRECTPARTS + DEFYSHIFTPARTS      );
      pol.setPoint(1, curPosX + sepPoint1, DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hrain );
      pol.setPoint(2, newPosX            , DEFHRECTPARTS + DEFYSHIFTPARTS - height_Hrain );
      pol.setPoint(3, newPosX            , DEFHRECTPARTS + DEFYSHIFTPARTS      );

      brush = painter->brush();
      if (num == curItem())
      	painter->setBrush(QBrush(QColor(30, 150, 30, transparency)));
      else
      	painter->setBrush(QBrush(QColor(20, 130, 20, transparency)));

      // pen.setColor(QColor(120, 120, 120));
      pen.setStyle(Qt::NoPen);
      // pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);

      if (items[num]->length() > 0)
	      painter->drawPolygon(pol);

      // pen.setColor(QColor(40, 40, 40));
      pen.setStyle(Qt::SolidLine);
      painter->setPen(pen);


      //Основной
      pol.setPoint(0, curPosX, curPosY+heightItem);
      pol.setPoint(1, curPosX, curPosY		 );
      pol.setPoint(2, newPosX, newPosY           );
      pol.setPoint(3, newPosX, newPosY+heightItem);

      brush = painter->brush();

      if (num == curItem())
	      deltaLight = 30;
      else
	      deltaLight = 0;

      if (
      	  (ttk->calcWaterConsumption() < ttk->crWaterConsumption()) &&
      	  (ttk->getVar(VAR_VS) < ttk->getVar(VAR_VR))		    &&
      	  (ttk->getVar(VAR_VR) < ttk->getVar(VAR_VD))
      	 )
      	painter->setBrush(QBrush(QColor(120, 120, 160+deltaLight)));
      else
      	painter->setBrush(QBrush(QColor(160+deltaLight, 120, 120)));

      if (items[num]->length() > 0)
	      painter->drawPolygon(pol);

      painter->setBrush(brush);

      pen.setStyle(Qt::DashLine);
      pen.setColor(QColor(40, 40, 40));
      painter->setPen(pen);
      painter->drawLine(newPosX, 0, newPosX, DEFHRECTPARTS+DEFYSHIFTPARTS);
      painter->setPen(QPen(Qt::SolidLine));

      brush = painter->brush();
      if (num == curItem())
      	painter->setBrush(QBrush(QColor(150, 150, 150)));
      else
	      painter->setBrush(QBrush(QColor(120, 120, 120)));

      pol.setPoint(0, curPosX, DEFYSHIFTPARTS);
      pol.setPoint(1, curPosX, 0	     );
      pol.setPoint(2, newPosX, 0             );
      pol.setPoint(3, newPosX, DEFYSHIFTPARTS);

      if (items[num]->length() > 0) {

      	painter->drawPolygon(pol);
      	painter->drawText(rint(curPosX + (newPosX-curPosX)/2 - 5), 15, QString().setNum(numItem));

      } //if (items[num]->length() > 0)

      painter->setBrush(brush);
      curPosX = newPosX;
      curPosY = newPosY;
      curPosY_r = newPosY_r;

    } //if (items[num] != NULL)

  } //for (int num = 0; num < MAXPARTSITEMS; num++)

  ttk->setVar(items[curItem()]->F(), VAR_F);
  ttk->setVar(items[curItem()]->i(), VAR_I);
  ttk->setVar(items[curItem()]->i(), VAR_IR);

  painter->setPen(oldPen);

} //void ChartParts::drawItems(QPainter * painter)

void ChartParts::draw(QPainter * painter) {

  QPen pen;

  pen.setColor(Qt::black);
  pen.setWidth(2);
  painter->setPen(pen);
//   painter->setBrush(QBrush(QColor(219, 219, 219)));
  painter->drawRect(0, 0, DEFWRECTPARTS+DEFXSHIFTPARTS, DEFHRECTPARTS+DEFYSHIFTPARTS);

  //Клетки
  drawCells(painter);

  //Участки
  drawItems(painter);

} //void ChartParts::draw(QPainter * painter)

ChartPartsItem *  ChartParts::item(int num) {

  if ((0 <= num) && (num < MAXPARTSITEMS)) {

    return items[num];

  } //if ((0 <= num) && (num < MAXPARTSITEMS))

  return NULL;
  
} //ChartPartsItem * ChartParts::item(int num)

void ChartParts::setItem(int num, ChartPartsItem * item) {

  if ((0 <= num) && (num < MAXPARTSITEMS)) {

    items[num] = item;

  } //if ((0 <= num) && (num < MAXPARTSITEMS))
  
} //void ChartParts::setItem(int num, ChartPartsItem * item)

void ChartParts::modifyItem(ChartPartsItem * item) {

  for (int num = 0; num < MAXPARTSITEMS; num++) {

    if (items[num] == item) {

      update();
      break;

    } //if (items[num] == item)

  } //for (int num = 0; num < MAXPARTSITEMS; num++)

} //void ChartParts::modifyItem(ChartPartsItem * item)

void ChartParts::deleteNum(int num) {

  if ((0 <= num) && (num < MAXPARTSITEMS)) {

    if (items[num] != NULL) {

      delete items[num];
      items[num] = NULL;

    } //if (items[num] != NULL)

  } //if ((0 <= num) && (num < MAXPARTSITEMS))

} //void ChartParts::deleteNum(int num)

void ChartParts::deleteItem(ChartPartsItem * item) {

  for (int num = 0; num < MAXPARTSITEMS; num++) {

    if (items[num] == item) {

      deleteNum(num);
      break;

    } //if (items[num] == item)

  } //for (int num = 0; num < MAXPARTSITEMS; num++)

} //void ChartParts::deleteItem(ChartPartsItem * item)

void ChartParts::resetItems() {

  for (int num = 0; num < MAXPARTSITEMS; num++)
    items[num] = new ChartPartsItem(0, 0, 0);
    // items[num] = NULL;

} //void ChartParts::resetItems()

double ChartParts::length() {

  double sumLength = 0;

  for (int num = 0; num < MAXPARTSITEMS; num++)
    if (items[num] != NULL)
      if (items[num]->length() > 0)
	sumLength += items[num]->length();

  return sumLength;

} //double ChartParts::length()

int ChartParts::itemCount() {

  int count = 0;

  for (int num = 0; num < MAXPARTSITEMS; num++)
    if (items[num] != NULL)
      count++;

  return count;

} //int ChartParts::itemCount()

double ChartParts::inclination() {

  double sumInc = 0;

  for (int num = 0; num < MAXPARTSITEMS; num++)
    if (items[num] != NULL)
      sumInc += items[num]->i();

  return sumInc;

} //double ChartParts::inclination()

double ChartParts::inclination_r() {

  double sumInc = 0;

  for (int num = 0; num < MAXPARTSITEMS; num++)
    if (items[num] != NULL)
      sumInc += items[num]->ir();

  return sumInc;

} //double ChartParts::inclination_r()

void ChartParts::mousePressEvent(QMouseEvent * event) {

  int pressX = event->x();

  if (event->buttons() & Qt::LeftButton) {

    for (int num=1; num < MAXPARTSITEMS; num++) {

      if (((xParts[num-1] <= pressX) && (pressX < xParts[num])) || (num == itemCount())){

	setCurItem(num-1);
	emit changedCurPart(num-1);
	break;

      } //if ((xParts[num-1] <= pressX) && (pressX < xParts[num]))

    } //for (int num=1; num < MAXPARTSITEMS; num++)

  } //if (event->buttons() & Qt::LeftButton)

} //void ChartParts::mousePressEvent(QMouseEvent *event)

// void ChartParts::keyPressEvent(QKeyEvent * event) {

// } //void ChartParts::keyPressEvent(QKeyEvent * event)
